<?php

$name = $_POST['txtName'];
$cat = $_POST['txtCat'];
$des = $_POST['txtDes'];
$price = $_POST['txtPrice'];

$thumb_name = $_FILES['fThumb']['name'];
$thumb_type = $_FILES['fThumb']['type'];
$thumb_tmp_name = $_FILES['fThumb']['tmp_name'];
$thumb_size = $_FILES['fThumb']['size'];
$thumb_error = $_FILES['fThumb']['error'];

if ($thumb_error == 0) {
    move_uploaded_file($thumb_tmp_name, "../products/thumbs/$thumb_name");
    $thumb = "products/thumbs/$thumb_name";
    echo "Insert to success!<br />";
}

mysql_connect('localhost', 'root', '');
mysql_select_db('gas');
$query = "insert into tbl_products(name, thumb, cat_id, description, price) values('$name', '$thumb', $cat, '$des', $price)";
mysql_query($query);
$last_id = mysql_insert_id();

$images_name = $_FILES['fImages']['name'];
$images_type = $_FILES['fImages']['type'];
$images_tmp_name = $_FILES['fImages']['tmp_name'];
$images_size = $_FILES['fImages']['size'];
$images_error = $_FILES['fImages']['error'];

foreach ($images_name as $nkey => $names) {
    $n[] = $names;
}
foreach ($images_tmp_name as $tnkey => $tmp_names) {
    $tmp_name[] = $tmp_names;
}
foreach ($images_error as $ekey => $errors) {
    $error[] = $errors;
}

for ($i = 0; $i < count($n); $i++) {
    if ($error[$i] == 0) {
        move_uploaded_file($tmp_name[$i], "../products/images/$n[$i]");
        $image = "products/images/$n[$i]";
        $query = "insert into tbl_images(pro_id, url) values($last_id, '$image')";
        mysql_query($query);
    }
}